package com.sec.etech.employeesync.handler;

import com.sec.etech.employeesync.entity.OrgEmployeeZNativePlace;
import com.sec.etech.employeesync.mapper.OrgEmployeeZNativePlaceMapper;
import com.sec.etech.employeesync.util.ConfigHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : 齐马的作品
 * @version : 1.0
 * @date : 2024/9/11 上午9:17
 * @description:
 */
@Component
@Slf4j
public class NativePlaceHandler implements ConfigHandler {

    @Resource
    private OrgEmployeeZNativePlaceMapper nativePlaceMapper;

    @Override
    public Long createConfig(String... params) {
        String name = params[0];

        OrgEmployeeZNativePlace nativePlace = new OrgEmployeeZNativePlace();
        nativePlace.setName(name);
        nativePlace.setActive(1);
        nativePlaceMapper.insert(nativePlace);

        log.info("新增籍贯配置:name={}", name);

        return nativePlace.getNativePlaceId();
    }

    @Override
    public List<ConfigEntry> getAllConfigs() {
        return nativePlaceMapper.selectList(null).parallelStream()
                .map(item -> {
                    ConfigEntry entry = new ConfigEntry();
                    entry.setId(item.getNativePlaceId());
                    entry.setParams(new String[]{item.getName()});
                    return entry;
                })
                .collect(Collectors.toList());
    }
}
